package com.project.action;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.project.entity.User;
import com.project.service.UserManage;

public class UserManageAction {
	private UserManage userManageService;
	private String jsonString;// 使用json传递数据
	private User user;
	private Integer userId;
	private	String pageMessage1,pageMessage2;
	
	
	public String getPageMessage1() {
		return pageMessage1;
	}
	public void setPageMessage1(String pageMessage1) {
		this.pageMessage1 = pageMessage1;
	}
	public String getPageMessage2() {
		return pageMessage2;
	}
	public void setPageMessage2(String pageMessage2) {
		this.pageMessage2 = pageMessage2;
	}
	public UserManage getUserManageService() {
		return userManageService;
	}
	public void setUserManageService(UserManage userManageService) {
		this.userManageService = userManageService;
	}
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	
	public String updateUser(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("user")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		if(user == null){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		User u = (User)context.getSession().get("user");
		user.setUserid(u.getUserid());
		this.userManageService.update(user);
		this.setJsonString("{success:true,maek:1}");
		user = this.userManageService.getById(u.getUserid());
		user.setUserpassword("");
		context.getSession().put("user", user);
		context.getSession().put("id", user.getUserid());
		context.getSession().put("name", user.getUsername());
		user = null;
		return "success";
	}
	
	public String changePass(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("user")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		if(pageMessage1 == null || pageMessage1.equals("") 
				|| pageMessage2 == null || pageMessage2.equals("")){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		User u = (User)context.getSession().get("user");
		int i = this.userManageService.changePass(pageMessage1, pageMessage2, String.valueOf(u.getUserid()));
		if(i == -1){
			this.setJsonString("{success:false,mark:-1}");
			return "success";
		}
		this.setJsonString("{success:true,mark:1}");
		return "success";
	}
	
	public String getUserById(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right") == null || !context.getSession().get("right").equals("user")){
			this.setJsonString("{success:false,mark:-2}");
			return "success";
		}
		User u = (User)context.getSession().get("user");
		JSONArray jsonObject = JSONArray.fromObject(u);
			this.setJsonString("{success:true,totalCount : " + 1
					+ ", list:" + jsonObject.toString() + "}");		
		return "success";
	}

}
